﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Web.PayChannal.Code;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;
using System.Text;
using ZLCMS.Business.Util;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Data.Model;
using DotNet.Common.Command;
using ZLCMS.Commands.Sys.Commands.Order;

namespace ZLCMS.Web.PayChannal.Weixinpay
{
    public partial class PaymentCallback : Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable requestHash = WXPayUtil.ParseXML(Request.InputStream);

            string payno = requestHash["out_trade_no"] as string;

            ShopPaymentInfo payConfig = null;
            using (NHibernate.ISession session = SessionFactoryHelper.CreateSession())
            {
                ShopOrderPayment orderPayment = session.CreateQuery(" from ShopOrderPayment where PaymentNo=:pno ")
                    .SetParameter("pno", payno)
                    .SetMaxResults(1)
                    .UniqueResult<ShopOrderPayment>();

                if (orderPayment == null)
                {
                    CMSKits.WriteLog("Weixinpay", "获取订单支付信息失败", payno);
                    Response.Write("fail");
                    Response.End();
                }


                payConfig = session.Get<ShopPaymentInfo>(orderPayment.PaymentId);

                if (payConfig == null)
                {
                    CMSKits.WriteLog("Weixinpay", "获取支付方式失败", payno);
                    Response.Write("fail");
                    Response.End();
                }
            }

            if (!WXPayUtil.CheckSign(requestHash, payConfig.PrivateKey))
            {
                CMSKits.WriteLog("Weixinpay", "验证签名失败", payno);
                Response.Write("fail");
                Response.End();
            }

            //获取交易状态
            string returnCode = requestHash["return_code"] as string;
            string resultCode = requestHash["result_code"] as string;
            //状态正常的时候才能调用PayOrder接口
            if (returnCode == "SUCCESS" && resultCode == "SUCCESS")
            {
                // !ConfirmPayment((decimal.Parse(total_fee) / 100), out_trade_no, transaction_id)
                string tradeNo = requestHash["transaction_id"] as string;
                decimal paymentFee = Convert.ToDecimal(requestHash["total_fee"]) / 100;
                ICommandResult<PayOrderResult> result = new PayOrderCommand().Execute(new PayOrderParameter()
                {
                    PaymentPrice = paymentFee,
                    PaymentNo = payno,
                    TradeNo = tradeNo
                });

                //todo记录支付日志
                if (!result.Error)
                {
                    CMSKits.WriteLog("system", "支付成功", "paymentNo=" + payno + ",tradeNo=" + tradeNo + ",totalprice=" + paymentFee);
                    Response.Write("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
                }
                else
                {
                    CMSKits.WriteLog("system", "支付失败", "paymentNo=" + payno + ",tradeNo=" + tradeNo + ",totalprice=" + paymentFee);
                }

            }
            else
            {
                CMSKits.WriteLog("system", "支付失败", "paymentNo=" + payno + ",returnCode=" + returnCode + ",resultCode=" + resultCode);
                Response.Write("fail");
            }
        }


    }
}